package net.noday.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.noday.domain.Article;
import net.noday.service.impl.ArticleServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
/**
 * 
 * @author Noday
 *
 */
@Controller
@RequestMapping("/article")
public class ArticleController {

	/** /article.get→index 获取列表 */
	@RequestMapping(method=RequestMethod.GET)
	public String index(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "article/index";
	}
	/** /article/page/{page}.get→index 分页获取列表 */
	@RequestMapping(value="/page/{page}", method=RequestMethod.GET)
	public String index_page(@PathVariable("page")int page, HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "article/index";
	}

	/** /article/new.get 进入新增 */
	@RequestMapping(value="/new", method=RequestMethod.GET)
	public String editNew(ModelMap model) {
		model.addAttribute("article", new Article());
		return "articlepage/edit";
	}
	
	/** /article.post 保存新增 表单提交 */
	@RequestMapping(method=RequestMethod.POST)
	public String create(@ModelAttribute("article") Article article, 
			BindingResult result, SessionStatus status) {
		new ArticleValidator().validate(article, result);
		if (result.hasErrors()) {
			return "article/add";
		} else {
			//保存
			if (articleService.add(article)) {
				status.setComplete();
			}
		}
		return "redirect:/";
	}
	/** /article/{id}.get 显示文章 */
	@RequestMapping(value="/{id}", method=RequestMethod.GET)
	public String show(@PathVariable("id") int id, HttpServletRequest request, 
			HttpServletResponse response) {
		//Article article = new Article();//读取
		
		return "articlepage";
	}
	/** /article/{id}/edit.get 进入编辑 */
	@RequestMapping(value="/{id}/edit", method=RequestMethod.GET)
	public String edit(@PathVariable("id") int id, HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "articlepage/edit";
	}

	/** /article/{id}.put 保存更新 */
	@RequestMapping(value="/{id}", method=RequestMethod.PUT)
	public String update(@PathVariable("id") int id, HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "";
	}
	/** /article/{id}.delete 删除 */
	@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
	public String delete(@PathVariable("id") int id, HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "";
	}
	
	/** /article.delete 批量删除 delete方法批量删除 */
	@RequestMapping(method=RequestMethod.DELETE)
	public String batchDelete(@RequestParam("items") int[] items, HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "";
	}

	/** 验证 */
	private static final class ArticleValidator implements Validator {

		@Override
		public boolean supports(Class<?> clazz) {
			return Article.class.isAssignableFrom(clazz);
		}

		@Override
		public void validate(Object target, Errors errors) {
			// 校验
			
		}
		
	}
	/** 获取相关对象 */
	@ModelAttribute("group")
	public List<String> getGroup() {
		return null;
	}
	
	/** 注入 */
	@Autowired
	private ArticleServiceImpl articleService;
}
